<template>
    <div>
        <!-- 售票汇总单 -->
        <div class="container">
            <!-- 表单 -->
            <el-form :inline="true" :model="formList" class="ticketForm" id="tichetingNote">
                <el-form-item label="售票时间起">
                    <el-date-picker type="datetime" size="mini" class="minipt" v-model="formList.startDate"
                                    default-time="00:00:00" value-format="yyyy-MM-dd-HH:mm:ss"></el-date-picker>
                    至
                    <el-date-picker type="datetime" size="mini" class="minipt" v-model="formList.endDate"
                                    default-time="23:59:59" value-format="yyyy-MM-dd-HH:mm:ss"></el-date-picker>
                </el-form-item>
                <el-form-item label="汇总单号">
                    <el-input size="mini" v-model="formList.salesumno" class="inp"></el-input>
                </el-form-item>
                <el-form-item label="售票员">
                    <el-autocomplete v-model="formList.salers" size="mini" class="inp"
                                     :fetch-suggestions="querySearchUser">
                        <template slot-scope="{ item }">
                            <div>[{{ item.code }}] {{ item.value }}</div>
                        </template>
                    </el-autocomplete>
                </el-form-item>
                <button class="check-btn rf" type="button" @click="check"><img
                        src="../../assets/imgs/operation_btn_icon_chaxun_default.png" alt="" class="btn-img">
                    查询
                </button>
            </el-form>
            <!-- 表格栏 -->
            <el-row>
                <el-col :span="24">
                    <avue-crud :data="banTable[0].data" :option="banTable[0].option" :page="this.banTable[0].page"
                               @size-change="handleSizeChange" @current-change="handleCurrentChange">
                    </avue-crud>
                </el-col>
            </el-row>
        </div>
    </div>
</template>
<script>
    import "../../assets/js/jquery.js";
    import qs from "qs";

    export default {
        data() {
            return {
                banTable: [
                    {
                        page: {
                            pageSizes: [10, 50, 100, 200],
                            total: "",
                            currentPage: 1,
                            pageSize: 50,
                        },
                        data: [],
                        option: {
                            page: true,
                            sumText: '合计',
                            border: true,
                            menu: false,
                            columnBtn: true,
                            index: true,
                            showSummary: true,
                            highlightCurrentRow: true,
                            height: 660,
                            align: 'center',
                            menuAlign: "center",
                            column: [],
                        }
                    }
                ],

                formList: {
                    startDate: "",
                    endDate: "",
                    salesumno: "",
                    salers: "",
                },
                salerslist: [],
                salersid: "",
            };
        },
        methods: {
            //下拉提示框回调
            createFilter(queryString) {
                return arr => {
                    return arr.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0;
                };
            },
            // 售票员自动补全
            querySearchUser(queryString, cb) {
                var arr = this.salerslist;
                var results = queryString
                    ? arr.filter(this.createFilter(queryString))
                    : arr;
                // 调用 callback 返回建议列表的数据
                cb(results);
            },
            //售票员请求
            loadUser() {
                var arr = [];
                this.$http
                    .post("/sys/user/listWithSpell")
                    .then(data => {
                        for (var c of data.data) {
                            arr.push({value: c.name, prop: c.id, code: c.no});
                        }
                    });
                return arr;
            },
            //进来获取列表
            getsalesumlist(){

                this.$http
                    .post("/query/salesum/detail/list",
                        qs.stringify({
                            filterRules: '[{"field":"sum.create_time","op":"dge","value":"'+this.formList.startDate+'"},' +
                            '{"field":"sum.create_time","op":"dle","value":"'+this.formList.endDate+'"}]',
                            page: this.banTable[0].page.currentPage,
                            rows: this.banTable[0].page.pageSize,
                        })
                    )
                    .then(data => {
                        this.banTable[0].page.total = data.data.totalRow;
                        this.banTable[0].page.currentPage = data.data.pageNumber;
                        this.banTable[0].page.pageSize = data.data.pageSize;
                        this.banTable[0].data = data.data.list;


                    })
            },
            //查询
            check(){
                this.controllernumb = 1;
                this.$http
                    .post("/query/salesum/detail/list",
                        qs.stringify({
                            filterRules: '[{"field":"sum.create_time","op":"dge","value":"'+this.formList.startDate+'"},' +
                            '{"field":"sum.create_time","op":"dle","value":"'+this.formList.endDate+'"},' +
                            '{"field":"sum.salesumno","op":"eq","value":"'+this.formList.salesumno+'"},' +
                            '{"field":"sum.seller_id","op":"eq","value":"'+this.salersid+'"}]',
                            page: this.banTable[0].page.currentPage,
                            rows: this.banTable[0].page.pageSize,
                        })
                    )
                    .then(data => {
                        this.banTable[0].page.total = data.data.totalRow;
                        this.banTable[0].page.currentPage = data.data.pageNumber;
                        this.banTable[0].page.pageSize = data.data.pageSize;
                        this.banTable[0].data = data.data.list;
                    })
            },
            //pageSize变化事件
            handleSizeChange(val) {
                console.log(val);
                this.banTable[0].page.pageSize = val;
                if (this.controllernumb == 1) {
                    this.check();
                } else {
                    this.getsalesumlist();
                }
            },
            //pageNum变化事件
            handleCurrentChange(val) {
                console.log(val);
                this.banTable[0].page.currentPage = val;
                if (this.controllernumb == 1) {
                    this.check();
                } else {
                    this.getsalesumlist();
                }
            },
            //时间处理,获取当天时间
            todayfunction(){
                let now = new Date();
                var nowday = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate())).toISOString().slice(0, 10);
                this.formList.startDate = nowday+" 00:00:00";
                this.formList.endDate = nowday+" 23:59:59";
            },
            //表头栏
            tableCreate() {
                this.banTable[0].option.column.push(
                    {
                        label: "汇总单号",
                        prop: "salesumno",
                        width: "200%"
                    },
                    {
                        label: "售票员",
                        prop: "seller_no",
                        width: "100px"
                    },
                    {
                        label: "售票数",
                        prop: "sale_count",
                    },
                    {
                        label: "售票金额",
                        prop: "sale_sum",
                        width: "80px"
                    },
                    {
                        label: "售票金额(非现金)",
                        prop: "sale_sum_noncash",
                        width: "130px"
                    },
                    {
                        label: "售外站票数",
                        prop: "sale_remote_count",
                        width: "95px"
                    },
                    {
                        label: "售外站票金额",
                        prop: "sale_remote_sum",
                        width: "110px"
                    },
                    {
                        label: "取票数",
                        prop: "lock_count",
                        width: "65px"
                    },
                    {
                        label: "取订票数",
                        prop: "book_count",
                        width: "80px"
                    },
                    {
                        label: "订票金额",
                        prop: "book_sum",
                        width: "80px"
                    },
                    {
                        label: "退票数",
                        prop: "refund_count",
                        width: "65px"
                    },
                    {
                        label: "退还金额",
                        prop: "refund_sum",
                        width: "80px"
                    },
                    {
                        label: "退还金额(非现金)",
                        prop: "refund_sum_noncash",
                        width: "130px"
                    },
                    {
                        label: "退票手续费",
                        prop: "refund_poundage",
                        width: "95px"
                    },
                    {
                        label: "废票数",
                        prop: "del_count",
                        width: "65px"
                    },
                    {
                        label: "废票金额",
                        prop: "del_sum",
                        width: "80px"
                    },
                    {
                        label: "废票金额(非现金)",
                        prop: "del_sum_noncash",
                        width: "130px"
                    },
                    {
                        label: "售票应缴金额",
                        prop: "paysum",
                        width: "110px"
                    },
                    {
                        label: "已缴",
                        prop: "is_payed",
                        width: "65px"
                    },
                    {
                        label: "使用票据合计",
                        prop: "",
                        width: "110px"
                    },
                    {
                        label: "票据用量(车票)",
                        prop: "ticket_bill_cost_count",
                        width: "120px"
                    },
                    {
                        label: "票据用量(免票儿童)",
                        prop: "enfant_bill_cost_count",
                        width: "150px"
                    },
                    {
                        label: "票据用量(退票凭证)",
                        prop: "refund_bill_cost_count",
                        width: "150px"
                    },
                    {
                        label: "票据起始号",
                        prop: "begin_no",
                        width: "100px"
                    },
                    {
                        label: "票据结束号",
                        prop: "end_no",
                        width: "100px"
                    },
                    {
                        label: "打印次数",
                        prop: "print_count",
                        width: "80px"
                    },
                    {
                        label: "最后打印用户",
                        prop: "print_no",
                        width: "110px"
                    },
                    {
                        label: "最后打印时间",
                        prop: "last_print_time",
                        width: "110px"
                    },
                );
            },
        },
        watch: {
            //侦听打单员，并转换id
            "formList.salers": {
                handler(curVal, oldVal){
                    console.log(curVal, oldVal);
                    if (this.formList.salers == "") {
                        this.salersid = "";
                    } else {
                        for (var item of this.salerslist) {
                            if (this.formList.salers == item.value) {
                                this.salersid = item.prop;
                                break;
                            } else {
                                this.salersid = "";
                            }
                        }
                        console.log(this.salersid)
                    }
                },
                deep: true,
            },
        },
        mounted(){
            this.todayfunction();
            this.salerslist = this.loadUser();
            this.tableCreate();
            this.getsalesumlist();
        },

    }
</script>
<style scoped src="../../assets/css/check/tichetingNote.css">

</style>

